package org.oschina.api.user.active.controller;

import java.util.List;
import org.oschina.api.BaseController;
import org.oschina.api.user.active.model.UserActiveType;
import org.oschina.api.user.active.service.UserActiveService;
import org.oschina.api.user.active.service.UserActiveTypeService;
import org.oschina.api.user.active.vo.UserActiveTypeVo;
import org.oschina.api.user.active.vo.UserActiveVo;
import org.oschina.bean.Pager;
import org.oschina.bean.Result;
import org.oschina.config.Config;
import org.oschina.handler.validator.annotation.Validate;
import org.oschina.handler.validator.annotation.ValidateField;
import org.oschina.handler.validator.filter.TextOnlyFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value=Config.API_PATH+"/user/active")
public class UserActiveController extends BaseController {

	@Autowired
	private UserActiveService userActiveService;
	@Autowired
	private UserActiveTypeService userActiveTypeService;
	
	/**
	 * 根据作者返回
	 * @param author
	 * @param pn
	 * @return
	 */
	@Validate
	@RequestMapping(value="/user/{author}",method=RequestMethod.GET)
	public Pager<UserActiveVo> findByAuthor
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer author,
		Integer pn
	)
	{
		if(pn==null || pn<=0)
			pn=1;
		
		return userActiveService.findByAuthor(author, pn);
	}
	
	/**
	 * 返回类型
	 * @return
	 */
	@RequestMapping(value="/type",method=RequestMethod.GET)
	public List<UserActiveTypeVo> findTypeVo()
	{
		return userActiveTypeService.findVo();
	}
	
	/**
	 * 更新
	 * @param userActiveType
	 * @return
	 */
	@Validate
	@RequestMapping(value="/type/update/{id}",method=RequestMethod.POST)
	public Result update
	(
		@Validate(validateField=
		{
			@ValidateField(name="id",validateValue=true,min=1),
			@ValidateField(name="name",filter={TextOnlyFilter.class}),
			@ValidateField(name="message",filter={TextOnlyFilter.class}),
			@ValidateField(name="url",filter={TextOnlyFilter.class})
		}) UserActiveType userActiveType
	)
	{
		return userActiveTypeService.update(userActiveType);
	}
}